package com.simple.action.fee;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.FeeReport;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.core.SessionBean;
import com.xunj.util.Common;

/**
 * 费用报销
 * @author 王瑾
 *
 */
public class FeeReimbursementAction extends AbstractAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -365972686059686745L;
	private FeeReport feeReport; //费用登记


	



	/**
	 * 查询核销完毕的数据
	 * @return
	 * @throws Exception
	 */
	public String listFeeReimbursement() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();

		paralist.add(new ParaUnit("createDate", ParaUnit.DESC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!feeReport.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", feeReport.getAreaId(), ParaUnit.EQ));
			if(!feeReport.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", feeReport.getAreaDeptId(), ParaUnit.EQ));
			
			if(!feeReport.getFeeReportYear().equals(""))
				paralist.add(new ParaUnit("feeReportYear", feeReport.getFeeReportYear(), ParaUnit.EQ));
			if(!feeReport.getFeeReportMonth().equals(""))
				paralist.add(new ParaUnit("feeReportMonth", feeReport.getFeeReportMonth(), ParaUnit.EQ));
			if(!feeReport.getReportState().equals(""))
				paralist.add(new ParaUnit("reportState", feeReport.getReportState(), ParaUnit.EQ));
			else
			{
				String[] state={"核销完毕","已报销"};
				paralist.add(new ParaUnit("reportState", state, ParaUnit.IN));
			}
			if(!feeReport.getFeeReportCode().equals(""))
				paralist.add(new ParaUnit("feeReportCode", feeReport.getFeeReportCode(), ParaUnit.LIKE));
			if(!feeReport.getRemitCompany().equals(""))
				paralist.add(new ParaUnit("remitCompany", feeReport.getRemitCompany(), ParaUnit.EQ));
			
			String remitDateFrom = request.getParameter("remitDateFrom");
			String remitDateTo = request.getParameter("remitDateTo");
			if(!remitDateFrom.equals(""))
			{
				paralist.add(new ParaUnit("remitDate",Common.newDate(remitDateFrom+" 00:00:00", "yyyy-MM-dd"), ParaUnit.GE));
				request.setAttribute("remitDateFrom", remitDateFrom);
			}
			if(!remitDateTo.equals(""))
			{
				paralist.add(new ParaUnit("remitDate",Common.newDate(remitDateTo+" 23:59:59", "yyyy-MM-dd"), ParaUnit.LE));
				request.setAttribute("remitDateTo", remitDateTo);
			}
			
		}
		else
		{
			String[] state={"核销完毕","已报销"};
			paralist.add(new ParaUnit("reportState", state, ParaUnit.IN));
		}
		List list = dao.criteriaByPage(FeeReport.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/fee/fee_reimbursement/lst_fee_reimbursement.jsp");
	}

	/**
	 * 报销数据
	 * @return
	 * @throws Exception
	 */
	public String updateFeeReimbursement() throws Exception
	{
		if(POST())
		{
			HttpServletRequest request = ServletActionContext.getRequest();
			SessionBean sessionBean  = SessionBean.getSessionBean(request);
			String feeReportId = request.getParameter("feeReportId");
			FeeReport fr = (FeeReport)dao.findOne(FeeReport.class, feeReportId);
			fr.setRemitCompany(feeReport.getRemitCompany());
			fr.setRemitCompanyName(feeReport.getRemitCompanyName());
			fr.setRemitCompanyName(feeReport.getRemitCompanyName());
			fr.setRemitDate(feeReport.getRemitDate());
			fr.setRemitUserId(sessionBean.getUserId());
			fr.setRemitUserName(sessionBean.getUserName());
			fr.setRemitState(feeReport.getRemitState());
			fr.setRemitRemark(feeReport.getRemitRemark());
			fr.setReportState("已报销");
			dao.update(fr);
			this.Alert_Close("保存成功", "ok");
			return toMessage();
		}
		else
		{
			String feeReportId = request.getParameter("feeReportId");
			feeReport = (FeeReport)dao.findOne(FeeReport.class, feeReportId);
			return toJsp("/jsp/fee/fee_reimbursement/update_fee_reimbursement.jsp");
		}
	}
	/**
	 * 撤销执行
	 * @return
	 * @throws Exception
	 */
	public String updateBackFeeReimbursement() throws Exception
	{
		String feeReportId = request.getParameter("feeReportId");
		FeeReport fr = (FeeReport)dao.findOne(FeeReport.class, feeReportId);
		fr.setReportState("已上报");
		fr.setWrittenOffDate(null);
		fr.setWrittenOffUserId(null);
		fr.setWrittenOffUserName(null);

		fr.setCertificateNoStart(null);
		fr.setCertificateNoEnd(null);
		dao.update(fr);
		//将费用核销明细字段置为核销完毕
		String hql = "update FeeDetail f set f.state = '上报' where f.feeReportId='"+fr.getFeeReportId()+"'";
		dao.bulkUpdate(hql);
		this.Alert_Close("保存成功", "ok");
		return toMessage();

	}
	
	public void setFeeReport(FeeReport feeReport) {
		this.feeReport = feeReport;
	}

	public FeeReport getFeeReport() {
		return feeReport;
	}


}